// // 这个注册全局组件的模块
// 这个模块需要导出一个对象或者函数, 如果导出的是一个对象, 需要提供install方法,
import XtxSkeleton from '@/components/skeleton/index.vue'
import XtxCarousel from '@/components/carousel/index.vue'
import XtxMore from '@/components/more/index.vue'
import XtxBread from '@/components/bread/index.vue'
import XtxBreadItem from '@/components/bread/item.vue'
import XtxCity from '@/components/city/index.vue'
import XtxNumbox from '@/components/numbox/index.vue'
import XtxButton from '@/components/button/index.vue'
import XtxCheckbox from '@/components/checkbox/index.vue'
import XtxMessage from '@/components/message/index.vue'
import XtxConfirm from '@/components/confirm/index.vue'
import XtxDialog from '@/components/dialog/index.vue'
import XtxSwitch from '@/components/switch/index.vue'
import XtxTabs from '@/components/tabs/index.vue'
import XtxTabsPane from '@/components/tabs/pane.vue'
import { App } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'
import defaultImg from '@/assets/images/200.png'
export default {
  // app如果实在不知道是什么类型 直接any即可
  // 其实vue提供app实例的类型 App, 需要单独导入一下
  install(app: App) {
    app.component(XtxSkeleton.name, XtxSkeleton)
    app.component(XtxCarousel.name, XtxCarousel)
    app.component(XtxMore.name, XtxMore)
    app.component(XtxBread.name, XtxBread)
    app.component(XtxBreadItem.name, XtxBreadItem)
    app.component(XtxCity.name, XtxCity)
    app.component(XtxNumbox.name, XtxNumbox)
    app.component(XtxNumbox.name, XtxNumbox)
    app.component(XtxButton.name, XtxButton)
    app.component(XtxCheckbox.name, XtxCheckbox)
    app.component(XtxMessage.name, XtxMessage)
    app.component(XtxConfirm.name, XtxConfirm)
    app.component(XtxDialog.name, XtxDialog)
    app.component(XtxSwitch.name, XtxSwitch)
    app.component(XtxTabs.name, XtxTabs)
    app.component(XtxTabsPane.name, XtxTabsPane)
    // 可以在install函数中注册全局组件，也可以注册全局指令
    app.directive('lazy', {
      mounted(el, binding) { // vue2中是inseted配置项,但是在vue3中叫mounted
        // console.log(el) // 使用v-lazy指令的dom元素, 就是图片标签
        // console.log(binding.value) // 图片的地址
        const { stop } = useIntersectionObserver(el, ([{ isIntersecting }]) => {
          if (isIntersecting) {
            el.src = binding.value
            stop()
            // 如果图片失败了 加载默认图片
            el.onerror = () => {
              el.src = defaultImg
            }
          }
        })
      }
    })
  }
}